package com.dataaccess.dataaccess.support.fluoriteCloud;

import com.ys.product.ysmq.front.log.Log;
import com.ys.product.ysmq.front.log.LogFactory;
import com.ys.product.ysmq.front.msg.AbstractConsumerMessage.ConsumerCallBack;
import com.ys.product.ysmq.front.msg.StandardConsumerMessage;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 读提交模式的示例demo
 * </p>
 * 
 * @version V1.0
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify by user: {修改人} 2018年5月31日
 * @modify by reason:{方法名}:{原因}
 */
public class MuiltiPartitionReadCommitMessagePatternMain {
    // 00d317f1511e41f985f2c34b9c057e00
    private static String appKey = "65a6b87b5e994b9dad18316fb9030153";

    // 6e9545953689729c612484ce1e9654a6
    private static String appSecret = "c9d3ec9adf2b314f000f78bb45a1a668";

    // private static Logger logger = LoggerFactory.getLogger(ReadCommitMessagePatternMain.class);
    private static String group = "group1";

    static int printListMaxSize = 100;

    private static Log log = LogFactory.getLog(ReadCommitMessagePatternMain.class);

    public static void main(String[] args) throws IOException {
        init(3);
    }

    public static void init(int partitionSize) {
        // 开放平台的url,这是test环境的url https://test.ys7.com:65
        String path = "https://open.ys7.com";
        //
        StandardConsumerMessage[] consumerMessages = new StandardConsumerMessage[partitionSize];
        for (int i = 0; i < partitionSize; i++) {
            // 设置你的appkey,appSecret,group
            StandardConsumerMessage consumerMessage = new StandardConsumerMessage(path, appKey, appSecret, group);
            // 设置调用接口的间隔时间,ms
            consumerMessage.setConsumeIntervalTime(1000);
            // 设置消费消息的回调接口,建议把消息放入本地队列,不要在回调函数里做业务处理.
            consumerMessage.setConsumerCallBack(new ConsumerCallBack() {
                @Override
                public void consumerCall(List<Object> msg) {
                    if (msg.size() > 100) {
                        // 消息太多,只打印前100个
                        log.info("msgSize:{},msg:{}", msg.size(), msg.subList(0, 100));
                    } else {
                        log.info("msgSize:{},msg:{}", msg.size(), msg);
                    }
                    for (Object object : msg) {
                        // class com.alibaba.fastjson.JSONObject
                        log.info("my readMessageHandle object:{},class:{}", object, object.getClass());
                    }
                }
            });
            consumerMessage.initClient();
            consumerMessages[i] = consumerMessage;
        }
    }
}
